import { getUserImgListServer } from '@/servers/app-servers';
import { UserImgItemType, userImgListAtom, userImgParamsAtom, userInfoAtom } from '@/store/user';
import { useAtom } from 'jotai';

// 自定义 Hook 封装获取用户图片的逻辑
export const useUserImgList = () => {
    // 当前用户信息
    const [userInfo] = useAtom(userInfoAtom);
    // 当前用户图片
    const [userImgList, setUserImgList] = useAtom(userImgListAtom);
    // 用户图片数据
    const [userImgParams, setUserImgParams] = useAtom(userImgParamsAtom);

    // 获取用户图片列表的函数
    const getUserImgList = async () => {
        const res = await getUserImgListServer(userInfo.user_id);
        const { code, data } = res;
        if (code === 0) {
            let { img_list } = data;
            if (img_list && img_list.length > 0) {
                img_list = img_list.map((item: UserImgItemType) => ({
                    id: item.id,
                    full_path: item.full_path,
                }));
                setUserImgList(img_list);
                setUserImgParams({
                    id: img_list[0].id,
                    full_path: img_list[0].full_path,
                });
            }
        }
    };

    return { getUserImgList};
};
